<?php
include_once "topo.php";
if(logado()){
    if($_SESSION["ci_user_nivel"] == 2){ header("location:index.php"); }
    $setor = new setor();
    
    if(valida::numero($_GET["id"])){
        $id = $_GET["id"];
        $setor->carregar($id);
    }
    
    if($_POST["btn_salvar"] == "Salvar"){
        $erro = 0;
        
        if(!$setor->set_descricao($_POST["descricao"])){ $erro = 1; }
        if(!$setor->set_email($_POST["email"])){ $erro = 1; }
        
        if($erro == 0){
            $id_setor = $setor->salvar();
            /*
            if($id_setor != 0){
                $sql = "delete from ci_rel_setor where id > 0 and (setor1 = '$id_setor' or setor1 = '$id_setor')";
                        
                $db = new db(config::$driver);
                $conexao = $db->conecta();
                $db->query($sql, $conexao);
                $db->close($conexao);
                
                $i = 0;
                while($_POST["setor"][$i]){
                    if(valida::numero($_POST["setor"][$i])){
                        $sql = "insert into ci_rel_setor(setor1, setor2) values('$id_setor', '".$_POST["setor"][$i]."')";
                        
                        $db = new db(config::$driver);
                        $conexao = $db->conecta();
                        $db->query($sql, $conexao);
                        $db->close($conexao);
                    }
                    $i++;
                }
            }
            */
            if($id != ''){
                $log = new log('ci_setor', $id_setor, "O usiário ".$_SESSION["ci_user_login"]." alterou o registro");
                $log->salvar();
                unset($log);
            }else{
                $log = new log('ci_setor', $id_setor, "O usiário ".$_SESSION["ci_user_login"]." criou o registro");
                $log->salvar();
                unset($log);
            }
            
            header("location: setor.php");
        }else{
            echo "
        <script>alert('Preencha todos os campos')</script>";
        }
    }
    
    if($id != ''){
        echo "
        <h3>Novo setor</h3>
        ";
    }else{
        echo "
        <h3>Alterar setor</h3>
        ";
    }
    ?>
        <form action="" method="post">
            <?php
            if($id == ''){
                echo "ID: ".$setor->get_id()."<br><br>";
            }
            ?>
            Descrição<br>
            <input type="text" name="descricao" value="<?=$setor->get_descricao()?>"><br><br>
            E-Mail para notificação<br>
            <input type="email" name="email" value="<?=$setor->get_email()?>"><br><br>
            <!--Setores para relacionar<br>-->
            <?php
            /*
            $sql = "select id, descricao from ci_setor";
            $db = new db(config::$driver);
            $conexao = $db->conecta();
            $result = $db->query($sql, $conexao);
            $db->close($conexao);
            
            while($setor = $db->fetch_array($result)){
                $sql_select = "select id from ci_rel_setor where (setor1 = '".$setor["id"]."' and setor2 = '$id') or (setor1 = '$id' and setor2 = '".$setor["id"]."')";
                $db1 = new db(config::$driver);
                $con = $db1->conecta();
                $res = $db1->query($sql_select, $con);
                $db1->close($con);

                $s = $db1->fetch_array($res);
                $checked = '';
                if($s[0] != ''){
                    $checked = "checked";
                }
                ?>
            <input type="checkbox" name="setor[]" value="<?=$setor["id"]?>" id="setor<?=$setor["id"]?>" <?=$checked?>>
            <label for="setor<?=$setor["id"]?>"><?=$setor["descricao"]?></label><br>
                <?php
            }
            */
            ?>
            <br>
            <button type="submit" name="btn_salvar" value="Salvar">Salvar</button>
        </form>
    <?php
    
    unset($setor);
    
}else{
    header("location: login.php");
}
include_once "rodape.php";
?>
